<!--suppress ALL -->
<template>

  <span>
    <list-cpt :head-items=this.headItems :data=this.data :paged=this.paged
              :content="content"
              :postUrl="userAddUrl" :updateUrl="userUpdateUrl" :deleteUrl="userDeleteUrl + '?type=1&ids='"
              @refreshPageSize="refreshPageSize" @refreshCurrentPage="refreshCurrentPage"
              @refresh="refresh"
              @echo="echo"
              @reset-post-content="resetPostContent">

      <template #add>
         <el-form ref="form" :model="content">
            <el-form-item label="用户名称">
              <el-input placeholder="请输入昵称" v-model="content.nickName"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input placeholder="请输入密码" v-model="content.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="重复密码">
              <el-input placeholder="请输入密码" v-model="content.repeatPassword" show-password></el-input>
            </el-form-item>
            <el-form-item label="联系电话">
              <el-input placeholder="请输入联系电话" v-model="content.mobile"></el-input>
            </el-form-item>
            <el-form-item label="邮箱地址">
              <el-input placeholder="请输入邮箱地址" v-model="content.email"></el-input>
            </el-form-item>
            <el-form-item label="头像地址">
              <el-input placeholder="请输入头像地址" v-model="content.portrait"></el-input>
            </el-form-item>
            <el-form-item label="用户信息备注">
              <el-input placeholder="请输入备注信息" v-model="content.memo"></el-input>
            </el-form-item>

           <el-form-item label="用户角色:">
            <el-select v-model="selectRoles" ref="elSelect" multiple placeholder="用户角色"
                       @visible-change="gainRoles"
                       @change="addCurrentRoles"
                       @remove-tag="removeCurrentRoles">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
           </el-form-item>

         </el-form>
      </template>

      <template #update>
         <el-form ref="form" :model="content">
            <el-form-item label="用户名称">
              <el-input placeholder="请输入昵称" v-model="content.nickName"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input placeholder="请输入密码" v-model="content.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="重复密码">
              <el-input placeholder="请输入密码" v-model="content.password" show-password></el-input>
            </el-form-item>
            <el-form-item label="联系电话">
              <el-input placeholder="请输入联系电话" v-model="content.mobile"></el-input>
            </el-form-item>
            <el-form-item label="邮箱地址">
              <el-input placeholder="请输入邮箱地址" v-model="content.email"></el-input>
            </el-form-item>
            <el-form-item label="头像地址">
              <el-input placeholder="请输入头像地址" v-model="content.portrait"></el-input>
            </el-form-item>
            <el-form-item label="用户信息备注">
              <el-input placeholder="请输入备注信息" v-model="content.memo"></el-input>
            </el-form-item>

           <el-form-item label="用户角色:">
            <el-select v-model="selectRoles" ref="elSelect" multiple placeholder="用户角色"
                       @visible-change="gainRoles"
                       @change="addCurrentRoles">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.label"
                :value="item.id">
              </el-option>
            </el-select>
           </el-form-item>
         </el-form>
      </template>

    </list-cpt>
  </span>

</template>

<script>
import ListCpt from '../../../components/ListCpt'
import {uaaUserItem} from '../../../components/Po'
import {url} from '../../../common/Common.js'
import {get} from '../../../components/HttpUtil'

export default {
  name: 'UaaUserPage',
  components: {ListCpt},
  data() {
    return {
      headItems: uaaUserItem,
      paged: true,
      currentPage: 1,
      pageSize: 10,
      count: 0,
      data: {},
      userListUrl: url.uaa.user.list,
      userAddUrl: url.uaa.user.add,
      userUpdateUrl: url.uaa.user.update,
      userDeleteUrl: url.uaa.user.delete,
      roleAllUrl: url.uaa.role.all,

      content: {
        nickName: '',
        password: '',
        repeatPassword: '',
        mobile: '',
        email: '',
        memo: '',
        roles: []
      },

      selectRoles: [],
      roles: [],
    }
  },

  methods: {
    gainList: function () {
      var url = this.userListUrl + '?size=' + this.pageSize + '&page=' + this.currentPage
      get(url, this.$store.state.token)
        .then(res => {
          this.data = res.data
        })
        .catch(e => {
          console.log(e)
        })
    },
    refreshPageSize(val) {
      this.currentPage = 1
      this.pageSize = val
      this.gainList()
    },

    refreshCurrentPage(val) {
      this.currentPage = val
      this.gainList()
    },

    gainRoles: function () {
      var url = this.roleAllUrl
      get(url, this.$store.state.token)
        .then(res => {
          this.$nextTick(function () {
            this.roles = res.data
          })
        })
        .catch(e => {
          console.log(e)
        })
    },

    addCurrentRoles(val) {
      this.content.roles = []
      val.forEach((e) => {
        this.content.roles.push({"id": e})
      })
      this.$refs.elSelect.blur()
    },

    removeCurrentRoles(val) {
      this.content.roles.pop(val[0])
      for (var i = 0; i < this.content.roles.length; i++) {
        if (this.content.roles[i].id == val) {
          this.content.roles.splice(i, 1)
        }
      }
      console.log(this.content.roles)
    },

    refresh() {
      this.gainList();
    },
    echo(data) {
      console.log(data)
      this.content = data;
      this.selectRoles = []
      for (var i = 0; i < this.content.roles.length; i++) {
        this.selectRoles.push(this.content.roles[i].id);
      }
    },
    resetPostContent() {
      this.content = {};
    }

  },

  created: function () {
    this.gainList()
  }
}


</script>

